/* Copyright (C) 2007-2008  klevgert  http://code.google.com/p/db4o-netbeans/

This file is part of the db4o-netbeans module suite.

db4o-netbeans is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation and as clarified by db4objects' GPL 
interpretation policy, available at
http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
Suite 350, San Mateo, CA 94403, USA.

db4o-netbeans is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */

package com.db4o.nb.api.impl;

import com.db4o.nb.api.Constrain;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Class representing a constrain as used later in a
 * db4o soda query.
 *
 * @author klevgert
 */
public class ConstrainImpl implements Constrain {
  
  /** underlying xml dom element */
  protected Element element;
  
  /** sub constraints, such as not, smaller, ... */
  protected List    children = new ArrayList();
  
  /** 
   * Creates a new instance of ConstrainImpl 
   * @param element   xml dom element to use.
   */
  public ConstrainImpl(Element element) {
    
    // Store xml element
    this.element = element;
    
    // Build child elements
    if( this.element != null ) {
      
      NodeList childNodes = this.element.getChildNodes();
      
      for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        if( childNode instanceof Element ) {
          Element childElement = (org.w3c.dom.Element)childNode;
          if( childElement.getTagName().equals( Constants.ELEM_DESCEND) ) {
            this.children.add( new DescendImpl(childElement) );
          } else if( childElement.getTagName().equals( Constants.ELEM_CONSTRAIN) ) {
            this.children.add( new ConstrainImpl(childElement) );
          }
        }
      }
    }
  }

  /**
   * Gets the tagging value of the constraint. 
   * A tagging value identifies the constraint as a value, 
   * or a relational operator, like not, greater, etc..
   *
   * @return tagging value.
   */
  public String getTag() {
    return this.element.getAttribute(Constants.ATTR_TAG);
  }
  
  /**
   * Gets the constraint type, as used in value constraints.
   * @return fully qualified java type.
   */
  public String getType() {
    return this.element.getAttribute(Constants.ATTR_TYPE);
  }

  /**
   * Gets the constraint value. 
   * May be a literal value or a operator.
   * @return constraint value.
   */
  public String getValue() {
    return this.element.getAttribute(Constants.ATTR_VALUE);
  }
  
  /**
   * Gets the sub constraints.
   * @return child elements as a list.
   */
  public List getChildren() {
    return children;
  } 

  /**
   * Adds a new child constraint. 
   * Call this method to add a sub constraint, because
   * the method also updates the xml element.
   * @param c   child contraint to add.
   */
  public void add(Constrain c) {
    
    this.element.appendChild( ((ConstrainImpl)c).element );
    this.children.add( c );
    
  }

  /**
   * Removes a new child constraint. 
   * Call this method to add a sub constraint, because
   * the method also updates the xml element.
   * @param c   child contraint to remove.
   */
  public void remove(Constrain c) {
    
    if( this.children.contains(c) ) {
      this.element.removeChild( ((ConstrainImpl)c).element );
      this.children.remove( c );
    }
  }

}
